﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MyMoviesBrowserLibrary;

using Microsoft.MediaCenter.UI;

namespace MyMoviesBrowser
{
    public class Trailer
    {
        string[] extensions = new string[] { "*.mov", "*.avi", "*.mpeg", "*.mpg", "*.mkv", "*.wmv", "*.mp4", "*.dvr-ms" };
        private BooleanChoice _checkBoxItem = new BooleanChoice();
        public BooleanChoice CheckBoxItem
        {
            get
            {
                if (_checkBoxItem == null)
                {
                    _checkBoxItem = new BooleanChoice();

                }
                return _checkBoxItem;
            }
            set
            {
                _checkBoxItem = value;
            }
        }

        public MovieTrailer MovieTrailer;

        public string Title { get { return MovieTrailer.Title; } }
        public string Runtime { get { return MovieTrailer.Runtime; } }
        public string Studio
        {
            get
            {
                if (MovieTrailer.Studio == null)
                    return "";
                else
                    return MovieTrailer.Studio.StudioName;
            }
        }
        public string Director
        {
            get
            {
                if (MovieTrailer.Director == null)
                    return "";
                else
                    return MovieTrailer.Director.PersonName;
            }
        }
        public string Rating
        {
            get
            {
                if (MovieTrailer.Rating == null)
                    return "";
                else
                    return MovieTrailer.Rating.RatingDesc;
            }
        }
        public string Description { get { return MovieTrailer.Description; } }
        public string GenreString
        {
            get
            {
                return string.Join(",", MovieTrailer.MovieTrailer_Genres.Select(g => g.Genre.GenreDesc).ToArray());
            }
        }
        public List<MediaFile> Files { get; set; }
        public List<CastMember> Cast { get; set; }
        public Image Image { get; set; }
        public bool ShowBackgroundImage
        {
            get
            {
                string imageLocation = Path.Combine(MovieTrailer.MovieFolder, "fanart.jpg");
                return File.Exists(imageLocation);
            }
        }
        public Image BackgroundImage
        {
            get
            {
                string imageLocation = Path.Combine(MovieTrailer.MovieFolder, "fanart.jpg");
                if (File.Exists(imageLocation))
                    return new Image("file://" + imageLocation);
                else
                    return new Image("resx://MyMoviesBrowser/MyMoviesBrowser.Resources/clear");
            }
        }

        public Trailer() { }
        public Trailer(MovieTrailer movieTrailer, System.Data.Linq.EntitySet<MovieTrailer_Genre> genres, System.Data.Linq.EntitySet<MovieTrailer_Person> persons)
        {
            MovieTrailer = movieTrailer;
            //DateUpdated = movieTrailer.DateUpdated;
            //Description = movieTrailer.Description;
            //Director = movieTrailer.Director;
            //DirectorPersonId = movieTrailer.DirectorPersonId;
            //ModifiedTime = movieTrailer.ModifiedTime;
            //MovieFolder = movieTrailer.MovieFolder;
            //MovieInfoId = movieTrailer.MovieInfoId;
            //MovieTrailer_Genres = genres;
            //MovieTrailer_Persons = persons;
            //MovieTrailerId = movieTrailer.MovieTrailerId;
            //PostDate = movieTrailer.PostDate;
            //PosterLocation = movieTrailer.PosterLocation;
            //Rating = movieTrailer.Rating;
            //RatingId = movieTrailer.RatingId;
            //ReleaseDate = movieTrailer.ReleaseDate;
            //Runtime = movieTrailer.Runtime;
            //SortTitle = movieTrailer.SortTitle;
            //Studio = movieTrailer.Studio;
            //StudioId = movieTrailer.StudioId;
            //Title = movieTrailer.Title;
            //Watched = movieTrailer.Watched;

            CheckBoxItem.Description = "Watched";
            CheckBoxItem.Value = MovieTrailer.Watched;
            CheckBoxItem.PropertyChanged += new PropertyChangedEventHandler(CheckBoxItem_PropertyChanged);
            MovieTrailer.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Trailer_PropertyChanged);
        }

        void Trailer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "Watched")
                MyMoviesBrowserLibrary.MyMoviesBrowserBusiness.MarkTrailerAsWatched(MovieTrailer, MovieTrailer.Watched);
        }

        public void GetFiles()
        {
            Files = new List<MediaFile>();
            DirectoryInfo folder = new DirectoryInfo(MovieTrailer.MovieFolder);
            List<FileInfo> _files = new List<FileInfo>();
            foreach (string ext in extensions)
                _files.AddRange(folder.GetFiles(ext));

            if (_files.Count == 1)
            {
                Files.Add(new MediaFile() { FileName = "Watch", Path = _files[0].FullName });
            }
            else
            {
                foreach (FileInfo file in _files.OrderBy(f => f.LastWriteTime))
                {
                    Files.Add(new MediaFile() { FileName = file.Name, Path = file.FullName });
                }
            }
        }

        void CheckBoxItem_PropertyChanged(IPropertyObject sender, string property)
        {
            if (property == "Value")
            {
                MovieTrailer.Watched = CheckBoxItem.Value;
            }
        }
    }
}
